<?php

class WRedis
{
    

    public $redis;

    public static $_instance = null;

    private function __construct()
    {
        // 连接redis
        $this->redis = new \Redis();
        $this->redis->connect('127.0.0.1',6379);
    
    }

    // 获取实例
    public static function getInstance()
    {
        if(is_null(self::$_instance) || empty(self::$_instance)){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    // 魔术方法

    public function __call($name,$arguments)
    {
        return $this->redis->$name(...$arguments);
    }

    public function get($key)
    {
        $value = $this->redis->get($key);
        if(!$value){
            return $value;
        }
        $data = @unserialize($value);
        if(is_array($data) || is_object($data))
        {
            return $data;
        }
        return $value;
    }

    public function set($key,$value)
    {
        if(is_array($value)  || is_object($value)){
            $value = @serialize($value);
        }
        return $this->redis->set($key,$value);
    }

}